package com.cyj.controller;

import com.cyj.enums.YesOrNo;
import com.cyj.pojo.Carousel;
import com.cyj.pojo.Category;
import com.cyj.pojo.vo.CategoryVO;
import com.cyj.pojo.vo.NewItemsVO;
import com.cyj.service.CarouselService;
import com.cyj.service.CategoryService;
import com.cyj.utils.IMOOCJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "首页",tags = {"首页展示的相关接口"})
@RestController
@RequestMapping("index")
public class IndexController {

    @Autowired
    private CarouselService carouselService;

    @Autowired
    private CategoryService categoryService;


    @ApiOperation(value = "获取首页轮播图列表",notes ="获取首页轮播图列表",httpMethod = "GET")
    @GetMapping("/carousel")
    public IMOOCJSONResult carousel(){

        List<Carousel> list = carouselService.queryAll(YesOrNo.Yes.type);

        return IMOOCJSONResult.ok(list);
    }

    @ApiOperation(value = "获取商品分类（以及分类）",notes ="获取商品分类（以及分类）",httpMethod = "GET")
    @GetMapping("/cats")
    public IMOOCJSONResult cats(){

        List<Category> list = categoryService.queryAllRootLevelCat();

        return IMOOCJSONResult.ok(list);
    }

    @ApiOperation(value = "获取商品子分类",notes ="获取商品子分类",httpMethod = "GET")
    @GetMapping("/subCat/{rootCatId}")
    public IMOOCJSONResult subCat(
            @ApiParam(name = "rootCatId",value = "一级分类id",required = true)
            @PathVariable Integer rootCatId){

        if( rootCatId == null){
            return IMOOCJSONResult.errorMsg("分类不存在");
        }

        List<CategoryVO> list =categoryService.getSubCatList(rootCatId);
        return IMOOCJSONResult.ok(list);
    }

    @ApiOperation(value = "查询每个一级分类下的最新6条商品数据",notes ="查询每个一级分类下的最新6条商品数据",httpMethod = "GET")
    @GetMapping("/sixNewItems/{rootCatId}")
    public IMOOCJSONResult sixNewItems(
            @ApiParam(name = "rootCatId",value = "一级分类id",required = true)
            @PathVariable Integer rootCatId){

        if( rootCatId == null){
            return IMOOCJSONResult.errorMsg("分类不存在");
        }

        List<NewItemsVO> list =categoryService.getSixNewItemsLazy(rootCatId);
        return IMOOCJSONResult.ok(list);
    }



}
